package ictbk.ttmk51.grapro.app.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileStringBase64 {
	public static String FiletoStringBase64(File file){
		byte [] b = new byte[(int)file.length()];
		String stringBase64;
		try{
			
			//Convert a file to byte[]
			FileInputStream fileInputStream = new FileInputStream(file);
			fileInputStream.read(b);
			
			//Convert to String Base64
			stringBase64 = Base64.encodeBytes(b);
			return stringBase64;
		}catch(FileNotFoundException e){
			e.printStackTrace();
			stringBase64 = null;
		}catch(IOException e1){
			e1.printStackTrace();
			stringBase64 = null;
		}
		return stringBase64;
	}
	public static File StringBase64toFile(String stringBase64, String pathfile){
		File result = new File(pathfile);
		
		try {
			Base64.decodeToFile(stringBase64, result.getName());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
